Suppose that you want to repeat a chunk of code many times, but changing one variable’s value each time you do it: This could be modifying each element of a vector in the same way, or analyzing a dataframe multiple times with different parameters.
_apply, map_ functions) allow you to take a function that solves the problem for a single input and generalize it to handle any number of inputs. This is very popular in R programming culture.for loop to repeat the chunk of code, and let it loop over the changing variable’s value. This is popular for many programming languages, but the R programming culture encourages a functional way instead.data.framesmap()map() takes in a vector or a list, and then applies the function on each element of it. The output is always a list.
purrr::map()map()We want to load up four data frames in the data/tumor/ directory using read_csv. Let’s try doing it for one first:
Now we build our list by listing the files in data/tumor/:
Now we can apply read_csv to each element in file_list. We can load these up by applying read_csv on each element of the file list.
Check what df_list[[1]] is:
plot_recurrence. Try it out with a data.framedf_list using read_csv.plot_recurrence to df_list using map()Write a function called plot_recurrence that plots days_to_last_follow_up vs. age_at_diagnosis. Try running it on lusc_data
read_csv on our list of file paths.Try applying plot_recurrence() to each element of df_list using map().
map()To be more specific about the output type, you can do this via the map_* function, where * specifies the output type: map_lgl(), map_chr(), and map_dbl() functions return vectors of logical values, strings, or numbers respectively.
Suppose you are working with the penguins dataframe:
and you want to look at the mean bill_length_mm for each of the three species (Adelie, Chinstrap, Gentoo).
We want to look at the mean bill_length_mm for each of the three species (Adelie, Chinstrap, Gentoo).
Adapt the below code into a function. Try it out on the first element of species_to_analyze()
analyze_bill <- function(species_to_analyze){
penguins_subset = filter(penguins, species == "Adelie")
out <- mean(penguins_subset$bill_length_mm, na.rm = TRUE)
return(out)
}
analyze_bill("Adelie")
c("Adelie", "Chinstrap", "Gentoo")map_dbl() outputs (double) numeric vectors.Apply analyze_bill to species_to_analyze:
More info at: https://adv-r.hadley.nz/functionals.html
A for loop repeats a chunk of code many times, once for each element of an input vector.
for (my_element in my_vector) {
chunk of code
}
Most often, the “chunk of code” will make use of my_element.
[1] 1 2 3 4
[1] 1
[1] 3
[1] 5
[1] 7